<template>
    <div>
        <navigation-bar>
            <div class="searchContent">
                服务名称：
                <el-select v-model="serveItem.service_guid" style="width: 100px" size="mini" @change="changeSelect">
                    <el-option
                        v-for="item in serveList"
                        :value="item.service_guid"
                        :key="item.service_guid"
                        :label="item.service_name"
                    ></el-option>
                </el-select>
                <custom-button type="add" style="margin-left: 10px" @addClick="addClick"></custom-button>
            </div>
        </navigation-bar>

        <div style="margin-top: 10px">
            <table-index
                :dataSource="dataSource"
                :tableColumns="tableColumns"
                :btnList="btnList"
                :pageSize="pageSize"
                :pageCount="pageCount"
                :pageNo="pageNo"
                @pageChange="pageChange"
                @editRow="editRow"
                @viewRow="viewRow"
                @deleteRow="deleteRow"
                selection
                :height="secondTableHeight"
            ></table-index>
        </div>

        <div v-if="dialogShow">
            <custom-dialog :title="dialogTitle" size="large" @dialogClose="dialogClose">
                <form-index
                    labelwidth="140px"
                    :formConfig="formConfig"
                    @submit="submit"
                    :type="type"
                    @formInputChange="formInputChange"
                    ref="form"
                >
                    <!--  <div>这里还可以插入其它内容进行拓展</div> -->
                </form-index>
            </custom-dialog>
        </div>
    </div>
</template>

<script>
import {serviceManageForm} from '@/assets/form/managementCenter/systemManage/serviceManage'
import customDialog from '@/components/customComponents/customDialog'
import customButton from '@/components/customComponents/customForm/customButton'
import formIndex from '@/components/customComponents/customForm/formIndex'
import tableIndex from '@/components/customComponents/customeTable/iviewTable/singleViewTable'
import navigationBar from '@/components/customComponents/managementCenter/navigationBar'
import {AjaxCommon, printAddress} from '@/libs/apiCommon'
import {CardId} from '@/libs/cardId'
import {PopupCommon} from '@/libs/popupCommon'
import {SessionCommon} from '@/libs/sessionCommon'
import {Utility} from '@/libs/utility'
import {OrgList} from '@/mockData/systemManage.js'
export default {
    props: ['buildInfo'],
    components: {
        navigationBar,
        customButton,
        tableIndex,
        customDialog,
        formIndex
    },
    // watch: {
    //     buildInfo: {
    //         handler(build) {
    //             if (build) {
    //                 this.buildItem = build
    //                 this.initPage()
    //             }
    //         },
    //         immediate: true
    //     }
    // },
    data() {
        return {
            serveList: [],
            serveItem: {},
            buildItem: {},
            secondTableHeight: document.body.clientHeight - 220,
            selectOrgList: [],
            // unitName: null,
            // table 相关参数  begin
            dataSource: [],
            tableColumns: [
                {
                    type: 'index',
                    width: 60,
                    align: 'center'
                },
                {
                    key: 'serviceip',
                    title: '监听服务器IP'
                },
                {
                    key: 'serviceport',
                    title: '监听服务器端口'
                },
                {
                    key: 'webip',
                    title: 'WEB监听IP'
                },

                {
                    key: 'webport',
                    title: 'WEB监听端口'
                },
                {
                    title: '操作',
                    slot: 'action',
                    width: 120,
                    align: 'center'
                }
            ],
            btnList: ['edit', 'delete'],
            pageSize: 20,
            pageCount: 100,
            pageNo: 1,
            // table 相关参数  end

            // 弹窗是否显示
            dialogShow: false,
            // 弹窗标题 (新增修改不同)
            dialogTitle: '',

            // form的配置信息
            formConfig: serviceManageForm,
            type: 'add',

            typeCodeList: []
        }
    },
    created() {
        Utility.getDictListByTypeCode('WGXH').then((res) => {
            if (res && res.length > 0) {
                const data = res.map((t) => {
                    return {
                        value: t.value,
                        label: t.desc
                    }
                })
                this.typeCodeList = data
            }
        })
    },
    methods: {
        changeSelect(e) {
            this.serveItem = this.serveList.find((t) => t.service_guid == e)
            this.pageNo = 1
            this.initPage()
        },
        getHeight() {
            this.secondTableHeight = document.body.clientHeight - 220
        },
        customClick(isTrue) {
            this.useOrg(isTrue)
        },
        formInputChange(e) {
            if (e.filedName == 'idCard') {
                const data = CardId.getInfoByCardId(e.value)
                if (data) {
                    let newData = e.allData
                    newData['birthday'] = data.birthday
                    newData['sex'] = data.sex
                    this.$refs.form.changeFormValue(newData)
                }
            }
        },
        submit(d) {
            let self = this
            console.log(d, 'd...............')

            if (d.CFG_GUID) {
                AjaxCommon('/api/ServiceConfig/EditConfigureInfo', 'post', d).then((res) => {
                    if (res?.data?.Code == 200) {
                        PopupCommon.notify('success', '编辑成功')
                        self.initPage()
                        self.dialogClose()
                    } else {
                        const msg = res?.data?.Message
                        if (msg) {
                            PopupCommon.notify('error', msg)
                        }
                    }
                })
            } else {
                delete d.CFG_GUID
                d.SERVICE_GUID = self.serveItem.service_guid
                d.IS_DELETE = 0
                // 根据类型调用不同接口
                let url = '/api/ServiceConfig/AddConfigureInfo'
                AjaxCommon(url, 'post', d).then((res) => {
                    if (res?.data?.Code == 200) {
                        PopupCommon.notify('success', '新增成功')
                        self.initPage()
                        self.dialogClose()
                    } else {
                        const msg = res?.data?.Message
                        if (msg) {
                            PopupCommon.notify('error', msg)
                        }
                    }
                })
            }
        },
        dialogClose() {
            this.dialogShow = false
        },
        // searchClick() {
        //     this.pageNo = 1
        //     this.initPage()
        // },
        // resetClick() {
        //     // this.unitName = null
        //     this.searchClick()
        // },
        addClick() {
            let formConfigItem = this.formConfig
            formConfigItem.map((t) => {
                Object.keys(t).map((res) => {
                    if (res == 'fieldName') {
                        t.filedValue = ''
                    }
                })
            })
            this.formConfig = formConfigItem
            this.type = 'add'
            this.dialogTitle = '新增'
            this.dialogShow = true
        },
        // 分页点击事件
        pageChange(p) {
            this.pageNo = p
            this.initPage()
        },
        editRow(d) {
            this.type = 'edit'
            let formConfigItem = this.formConfig
            formConfigItem.map((t) => {
                Object.keys(t).map((res) => {
                    if (t.fieldName == 'CFG_GUID') {
                        t.filedValue = d['id']
                    } else if (t.fieldName == 'MONITOR_IP') {
                        t.filedValue = d['serviceip']
                    } else if (t.fieldName == 'SERVICE_PORT') {
                        t.filedValue = d['serviceport']
                    } else if (t.fieldName == 'WEB_IP') {
                        t.filedValue = d['webip']
                    } else if (t.fieldName == 'WEB_PORT') {
                        t.filedValue = d['webport']
                    }
                })
            })
            this.formConfig = formConfigItem
            this.dialogTitle = '编辑'
            this.dialogShow = true
        },
        viewRow(d) {
            this.type = 'look'
            let formConfigItem = this.formConfig
            formConfigItem.map((t) => {
                Object.keys(t).map((res) => {
                    if (res == 'fieldName') {
                        t.filedValue = d.data[t[res]]
                    }
                })
            })
            this.formConfig = formConfigItem
            this.dialogTitle = '查看'
            this.dialogShow = true
        },
        // 启用或禁用组织网关 isTrue  true禁用 false 不禁用
        useOrg(isTrue) {
            const selectOrgList = this.selectOrgList
            if (selectOrgList.length < 1) {
                PopupCommon.notify('info', '请选择至少一个网关')
                return
            }
            const id = selectOrgList.map((t) => t.id).toString()
            let self = this
            let apiUrl = ''
            let tipMsg = ''
            if (!isTrue) {
                apiUrl = `api/OrgSysManage/ChangeOrgsDisable`
                tipMsg = '启用'
            } else {
                apiUrl = 'api/OrgSysManage/ChangeOrgsEnable'
                tipMsg = '禁用'
            }
            AjaxCommon(apiUrl, 'post', {orgids: id}).then((res) => {
                console.log(res, 'res...............')
                if (res?.data?.Code == 200) {
                    PopupCommon.notify('success', tipMsg + '成功')
                    self.initPage()
                } else {
                    const msg = res?.data?.Message
                    if (msg) {
                        PopupCommon.notify('error', msg)
                    }
                }
            })
        },
        deleteRow(d) {
            const id = d.id

            let self = this
            AjaxCommon(`/api/ServiceConfig/DeleteConfigInfo/${id}`, 'post', {}).then((res) => {
                console.log(res, 'res...............')
                if (res?.data?.Code == 200) {
                    PopupCommon.notify('success', '删除成功')
                    self.initPage()
                } else {
                    const msg = res?.data?.Message
                    if (msg) {
                        PopupCommon.notify('error', msg)
                    }
                }
            })
        },
        // 初始化事件
        initPage() {
            let self = this
            AjaxCommon(
                `/api/ServiceConfig/GetConfigureInfos?sid=${this.serveItem.service_guid}&PageSize=${this.pageSize}&PageIndex=${this.pageNo}`,
                'get',
                null
            ).then((res) => {
                if (res?.data?.Code == 200) {
                    const data = res?.data.Message
                    self.dataSource = data.rows
                    self.pageCount = parseInt(data?.total)
                } else {
                    PopupCommon.notify('error', res?.data?.msg)
                }
            })
        },
        async initServe() {
            const serveList = await AjaxCommon(`/api/ServiceConfig/GetServices`, 'get', null)
            this.serveList = serveList.data?.Message
            this.serveItem = serveList.data?.Message?.[0]
            this.initPage()
        }
    },
    mounted() {
        this.$nextTick(() => {
            this.getHeight()
            this.initServe()
        })

        /*窗口自适应，关键代码*/
        window.onresize = () => {
            this.getHeight()
        }
    }
}
</script>

<style></style>
